package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.service.impl.ArticlesStarServiceImpl;
import com.kykj.kykj.vo.ArticlesStarVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/articles/")
@CrossOrigin//解决跨域问题
@RestController
public class ArticlesStarController {
    @Resource
    ArticlesStarServiceImpl articlesStarService;

    /**
     * 获取收藏列表
     * @return resultEntity
     */
    @GetMapping("getArticlesStarList")
    private ResultEntity getArticlesLikeList(){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setData(articlesStarService.getArticlesStarList());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 增加收藏
     * @param articlesStarVo
     * @return resultEntity
     */
    @PostMapping("articlesstar")
    private ResultEntity addArticlesStar(@RequestBody ArticlesStarVo articlesStarVo){
        ResultEntity resultEntity = new ResultEntity();
        try{

            if(articlesStarService.addArticlesStar(articlesStarVo)>0){
                System.out.println("当前自增的id是"+articlesStarVo.getId()+"------自增");
                resultEntity.setCode(0);
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("添加失败！"+articlesStarVo.getId());
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }

    /**
     * 删除收藏
     * @param id
     * @return
     */
    @DeleteMapping("articlesstar/{id}")
    private ResultEntity delStar(@PathVariable("id") int id){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesStarService.delStar(id)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }
    /**
     * 修改收藏
     * @param articlesStarVo
     * @return
     */
    @PutMapping("articlesstar")
    private ResultEntity upArticlesStar(@RequestBody ArticlesStarVo articlesStarVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articlesStarService.upArticlesStar(articlesStarVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }
}
